/* 
 * Copyright TRL Team, Inc. All Rights Reserved.
 */

/* global vis */

TLR.services
.factory('UserSrv', function ($resource) {
    return $resource("device/rest/user/:action", {
        action: '@action'
    }, {
        login: {method: "POST", params: {action: 'login'}, isArray: false},
        logoff: {method: "GET", params: {action: 'logoff'}, isArray: false},
        unload: {method: "GET", params: {action: 'unload'}, isArray: false},
        checkLogin: {method: "GET", params: {action: 'check_login'}, isArray: false},
        changepwd: {method: "POST", params: {action: 'changepwd'}, isArray: false},
        register: {method: "POST", params: {action: 'register'}, isArray: false},
        update: {method: "POST", params: {action: 'update'}, isArray: false},
        list: {method: "GET", params: {action: 'list'}, isArray: false},
        delete: {method: "POST", params: {action: 'del'}, isArray: false},
        get: {method: "GET", params: {action: 'get'}, isArray: false},
        verify_code: {method: "GET", params: {action: 'send_verifycode'}, isArray: false},
        sign: {method: "GET", params: {action: "wechat_sign"}, isArray: false}
    });
})
.factory('DocManageSrv', function ($resource) {
    return $resource("device/rest/doc/list", {
    }, {
        list: {method: "GET", isArray: false}
    });
})
.factory('DeviceSrv', function ($resource) {
    return $resource("device/rest/general_device/:action", {
        action: '@action'
    }, {
        list: {method: "GET", params: {action: 'list'}, isArray: true},
        add: {method: "POST", params: {action: 'add'}, isArray: false},
        modify: {method: "POST", params: {action: 'modify'}, isArray: false},
        del: {method: "POST", params: {action: 'del'}, isArray: false},
        rename: {method: "POST", params: {action: 'rename'}, isArray: false},
        del_bind: {method: 'DELETE', params: {action: "del_bind"}, isArray: false},
        binded_user: {method: 'GET', params: {action: "binded_user"}, isArray: false},
        get_bind: {method: 'GET', params: {action: "get_bind"}, isArray: false}
    });
})
.factory('AirDeviceSrv', function ($resource) {
    return $resource("device/rest/air_measure/:action/:mac", {
        action: '@action',
        mac: "@mac",
        id: "@id",
        data_type: "@data_type"
    }, {
        last_data: {method: "GET", params: {action: 'last_data'}, isArray: false},
        last_bind_data: {method: "GET", params: {action: 'last_bind_data'}, isArray: false},
        aqi: {method: "GET", params: {action: 'aqi'}, isArray: false},
        day_history: {method: "GET", params: {action: 'day_history'}, isArray: false},
        hour_history: {method: "GET", params: {action: 'hour_history'}, isArray: false},
        add_bind: {method: "POST", params: {action: 'add_bind'}, isArray: false},
        edit_bind: {method: "POST", params: {action: 'edit_bind'}, isArray: false},
        update_position: {method: "POST", params: {action: 'update_position'}, isArray: false}
    });
})
.factory('AirDeviceMockSrv', function ($resource) {
    return $resource("app/mock/air_info.json", {}, {
        list: {method: "GET", isArray: false}
    });
})
.factory('EBanDeviceSrv', function ($resource) {
    return $resource("device/rest/health_device/:param", {
        param: "@param"
    }, {
        last_basic_data: {method: 'GET', params: {param: "last_basic_data"}, isArray: false},
        last_stat_data: {method: 'GET', params: {param: "last_stat_data"}, isArray: false},
        day_stat: {method: 'GET', params: {param: "day_stat"}, isArray: false},
        week_stat: {method: 'GET', params: {param: "week_stat"}, isArray: false},
        day_detail_stat: {method: 'GET', params: {param: "day_detail_stat"}, isArray: false},
        stat_type: {method: 'GET', params: {param: "stat_type"}, isArray: false},
        map_info: {method: 'GET', params: {param: "map_info"}, isArray: false},
        fast_map_info: {method: 'GET', params: {param: "fast_map_info"}, isArray: false},
        binded: {method: 'GET', params: {param: "binded"}, isArray: false},
        alert_list: {method: 'GET', params: {param: "alert_list"}, isArray: false},
        alert_relations: {method: 'GET', params: {param: "alert_relations"}, isArray: false},
        add_relation: {method: 'POST', params: {param: "add_relation"}, isArray: false},
        del_relation: {method: 'DELETE', params: {param: "del_relation"}, isArray: false},
        service_address: {method: 'GET', params: {param: "ebanservice"}, isArray: false},
        add_bind: {method: 'POST', params: {param: "add_bind"}, isArray: false},
        get_bind: {method: 'GET', params: {param: "get_bind"}, isArray: false},
        get_exist_health_user: {method: 'GET', params: {param: "get_exist_health_user"}, isArray: false},
        edit_bind: {method: 'POST', params: {param: "edit_bind"}, isArray: false},
        save_desc: {method: 'POST', params: {param: "save_desc"}, isArray: false},
        close_case: {method: 'POST', params: {param: "close_case"}, isArray: false},
        update_urgent: {method: 'GET', params: {param: "update_urgent"}, isArray: false},
        tlr_urgent_relations: {method: 'GET', params: {param: "tlr_urgent_relations"}, isArray: false},
        all_health_user: {method: 'GET', params: {param: "all_health_user"}, isArray: false},
        edit_health_user: {method: 'POST', params: {param: "edit_health_user"}, isArray: false}
    });
})
.factory('EBanDeviceMockSrv', function ($resource) {
    return $resource("app/mock/health_info.json", {}, {
        list: {method: "GET", isArray: false}
    });
})
.factory('AirDeviceChart', function () {
    return {
        create: function (historyData) {
            var items = [], items2 = [];
            for (var i = 0; i < (24 > historyData.length ? historyData.length : 24); ++i) {
                var data = historyData[i];
                var date = data.history_hour ? data.history_year + '-' + data.history_month + '-' + data.history_day + ' ' + data.history_hour + ':00:00'
                : data.history_year + '-' + data.history_month + '-' + data.history_day;
                items.push({
                    x: date,
                    y: data.pm25_measure,
                    group: 0
                });
                items2.push({
                    x: date,
                    y: data.pm25_outdoor,
                    group: 1
                });
            }
            var container = document.getElementById('id-air-measure-day-history');
            var groups = new vis.DataSet();
            groups.add({id: 0, content: "室内PM2.5"});
            groups.add({id: 1, content: "室外PM2.5"});
            var start = items[8].x, end = items[0].x;
            var dataset = new vis.DataSet();
            dataset.add(items);
            dataset.add(items2);
            var options = {
                start: start,
                end: end,
                legend: true,
                orientation: 'top',
                style: 'bar',
                barChart: {width: 50, align: 'center', sideBySide: true}, // align: left, center, right
                drawPoints: false,
                format: {minorLabels: {
                        millisecond: 'SSS',
                        second: 's',
                        minute: 'HH:mm',
                        hour: 'HH:mm',
                        weekday: 'MM-DD',
                        day: 'DD',
                        month: 'MM',
                        year: 'YYYY'
                    },
                    majorLabels: {
                        millisecond: 'HH:mm:ss',
                        second: 'YYYY-MM-DD HH:mm',
                        minute: 'YYYY-MM-DD',
                        hour: 'YYYY-MM-DD',
                        weekday: 'YYYY-MM',
                        day: 'YYYY-MM-DD',
                        month: 'YYYY-MM',
                        year: 'YYYY'
                    }
                }

            };
            this.graph2d = new vis.Graph2d(container, dataset, groups, options);
        }
    };
})
.factory('EbanReportSrv', function () {
    return {
        weekReport: function (id, ec, shape, title, labelName, data) {
            var labels = [
                {name: labelName, value: data[0], xAxis: 0, yAxis: data[0]},
                {name: labelName, value: data[1], xAxis: 1, yAxis: data[1]},
                {name: labelName, value: data[2], xAxis: 2, yAxis: data[2]},
                {name: labelName, value: data[3], xAxis: 3, yAxis: data[3]},
                {name: labelName, value: data[4], xAxis: 4, yAxis: data[4]}
            ];
            var option = {
                title: {
                    text: title,
                    textStyle: {
                        color: '#fff'
                    }
                },
                tooltip: {
                    trigger: 'axis'
                },
                calculable: true,
                xAxis: [
                    {
                        axisLine: {lineStyle: {color: "#fff"}},
                        axisLabel: {textStyle: {color: "white"}},
                        type: 'category',
                        boundaryGap: false,
                        data: ['周一', '周二', '周三', '周四', '周五']
                    }
                ],
                yAxis: [
                    {
                        axisLine: {lineStyle: {color: "#fff"}},
                        type: 'value',
                        axisLabel: {
                            textStyle: {color: "white"},
                            formatter: '{value}'
                        }
                    }
                ],
                series: [
                    {
                        barWidth: 16,
                        name: labelName,
                        type: shape,
                        data: data,
                        markPoint: {
                            data: labels
                        },
                        itemStyle: {
                            normal: {barBorderRadius: 0}
                        }
                    }
                ]
            };
            this.myChart = ec.init(document.getElementById(id), "macarons");
            window.onresize = this.myChart.resize;
            this.myChart.setOption(option, true);
        }
    };
})
.factory('EBanDeviceDetailSrv', function () {
    return {
        create: function (id, onSelect, stack, height, data) {
            $("#" + id).empty();
            var container = document.getElementById(id);
            // Create a DataSet (allows two way data-binding)
            var items = new vis.DataSet(data);

            // Configuration for the Timeline
            var options = {
                height: height,
                width: 1000,
                align: "center",
                showCurrentTime: true,
                stack: stack,
                format: {minorLabels: {
                        millisecond: 'SSS',
                        second: 's',
                        minute: 'HH:mm',
                        hour: 'HH:mm',
                        weekday: 'MM-DD',
                        day: 'DD',
                        month: 'MM',
                        year: 'YYYY'
                    },
                    majorLabels: {
                        millisecond: 'HH:mm:ss',
                        second: 'YYYY-MM-DD HH:mm',
                        minute: 'YYYY-MM-DD',
                        hour: 'YYYY-MM-DD',
                        weekday: 'YYYY-MM',
                        day: 'YYYY-MM-DD',
                        month: 'YYYY-MM',
                        year: 'YYYY'
                    }
                }
            };

            // Create a Timeline
            var timeline = new vis.Timeline(container, items, options);
            timeline.setOptions({orientation: {axis: "top"}});
            timeline.setOptions({orientation: {item: "bottom"}});
            if (onSelect) {
                timeline.on("select", function (props) {
                    onSelect(items._data[props.items[0]]);
                });
            }
        }
    };
})
.factory('CarSrv', function ($resource) {
    return $resource("device/rest/location_device/:action", {
        action: '@action'
    }, {
        list: {method: "GET", params: {action: "locations"}, isArray: false}
    });
})
.factory('CarMockSrv', function ($resource) {
    return $resource("app/mock/location_card.json", {}, {
        list: {method: "GET", isArray: false}
    });
})
.factory('GaoDeMapSrv', function () {
    return {
        devKey: "b8e3ce7528c82a04f1edb15c3dd9304f",
        buildMap: function () {
        }
    };
})
.factory('UserCenterSrv', function ($resource) {
    return $resource("assessment/rest/action/:action", {
        action: '@action'
    }, {
        login: {method: "POST", params: {action: 'login'}, isArray: false},
        register: {method: "POST", params: {action: 'register'}, isArray: false},
        bind: {method: "POST", params: {action: 'bind'}, isArray: false},
        updatepolicy: {method: "POST", params: {action: 'updatepolicy'}, isArray: false}
    });
})
.factory('BodyMapSrv', function ($resource) {
    return $resource("device/rest/bodymap/list", {
    }, {
        list: {method: "GET", params: {param: "assessment"}, isArray: false}
    });
})
.factory('AssessmentSrv', function ($resource) {
    return $resource("assessment/rest/action/:action", {
        action: '@action'
    }, {
        add: {method: "POST", params: {action: 'createassessment'}, isArray: false},
        query: {method: "POST", params: {action: 'queryassessment'}, isArray: false},
        modify: {method: "POST", params: {action: 'modifyassessment'}, isArray: false},
        get: {method: "POST", params: {action: 'getassessment'}, isArray: false},
        annotate: {method: "POST", params: {action: 'annotateassessment'}, isArray: false},
        listassessment: {method: "GET", params: {action: 'listassessment'}, isArray: true}
    });
})
.factory('VehicleSrv', function ($resource) {
    return $resource("device/rest/vehicle/:action", {
        action: '@action'
    }, {
        update: {method: "POST", params: {action: 'position'}, isArray: false},
        positions: {method: "GET", params: {action: 'position'}, isArray: false},
        set_status: {method: "GET", params: {action: 'set_status'}, isArray: false},
        get_status: {method: "GET", params: {action: 'get_status'}, isArray: false}
    });
});
